
from NewBuildingOperator import NewBuildingOperator
from PageOperator import PageOperator
from LoginManager import LoginManager
import time
import threading



class LoginAutoTimer(threading.Thread):
    def __init__(self, game_login_url, username, password, village_urls, building_urls, build_sequence, interval, max_runs):
        super().__init__()
        self.game_login_url = game_login_url
        self.username = username
        self.password = password
        self.village_urls = village_urls
        self.building_urls = building_urls
        self.build_sequence = build_sequence
        self.interval = interval
        self.max_runs = max_runs
        self.run_count = 0
        self.stop = False

    def run(self):
        while not self.stop and self.run_count < self.max_runs:
            login_manager = LoginManager(self.game_login_url, self.username, self.password)
            driver = login_manager.login()
            if driver:
                page_operator = PageOperator(driver, self.village_urls)
                page_operator.check_and_upgrade_resources()

                new_building_operator = NewBuildingOperator(driver, self.building_urls, self.build_sequence)
                new_building_operator.check_and_upgrade_buildings()

                driver.quit()
            self.run_count += 1
            print(f"已运行 {self.run_count} 次，等待 {self.interval} 秒后继续...")
            for i in range(self.interval, 0, -5):
                print(f"{i}", end=" ")
                time.sleep(5)
            time.sleep(1)
        print(f"程序已运行 {self.max_runs} 次，自动停止。")